package com.yyfax.ph.progress.detail;

import android.os.Parcel;
import android.os.Parcelable;
import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Model;
import com.yyfax.infrastructure.network.NetworkHelper;
import com.yyfax.infrastructure.network.entity.BaseResultEntity;
import com.yyfax.ph.base.YJPH;
import retrofit2.http.Body;
import retrofit2.http.Header;
import retrofit2.http.POST;
import rx.Observable;

import java.util.ArrayList;
import java.util.List;

public class ProgressDetailModel extends Model {

	@Override
	public void onNext(Object obj) {
		BaseResultEntity entity = cast(obj);
		switch (entity.requestCode){
			case ProgressDetailPresenter.GET_PROGRESS_DETAIL:
				DetailResult detailResult = cast(obj);
				EventAgent.post(detailResult);
				break;
			case ProgressDetailPresenter.GET_PROGRESS_HITSTORY:
				HistoryResult historyResult = cast(obj);
				EventAgent.post(historyResult);
				break;
		}
	}

	@Override
	public Observable getObservable(Object params, int functionalCode) {
		Service service = (Service) NetworkHelper.createService(Service.class);
		String token = YJPH.getInstance().getPreferenceManager().getLoginToken();
		switch (functionalCode){
			case ProgressDetailPresenter.GET_PROGRESS_DETAIL:
				return service.getDetail(token, (GetDetailParams) params);
			case ProgressDetailPresenter.GET_PROGRESS_HITSTORY:
				return service.getProgressHistory(token, (ProgressHistoryParams) params);
		}
		return null;

	}

	private interface Service {
		@POST("apply/query/getProgressDetail")
		Observable<DetailResult> getDetail(@Header("token") String token, @Body GetDetailParams params);

		@POST("apply/query/getHistory")
		Observable<HistoryResult> getProgressHistory(@Header("token") String token, @Body ProgressHistoryParams params);
	}


	public static class GetDetailParams {
		/** 推荐编号 */
		public String rcmdId = "";
	}

	public static class ProgressHistoryParams {
		/** 推荐编号 */
		public String rcmdId = "";
	}

	public static class ProgressHistory {

		public List<History> history = new ArrayList<>();
	}


	static class History {
		/** 状态名称 */
		String statusDesc = "";
		/** 更新时间 */
		String updateDate = "";
		/** 节点样式（1：中间节点 2：最后节点（未结束） 3：最后节点（已结束）） */
		String nodeType = "";
	}

	public static class HistoryResult extends BaseResultEntity<ProgressHistory> {
	}

	public static class DetailResult extends BaseResultEntity<DetailBean> {
	}

	public static class DetailBean implements Parcelable {
		/** 状态 */
		public String status = "";
		/** 推荐时间 */
		public String rcmdTime = "";
		/** 状态提示 */
		public String statusTip = "";
		/** 客户姓名 */
		public String name = "";
		/** 身份证 */
		public String certId = "";
		/** 手机号 */
		public String phone = "";
		/** 常住城市 */
		public String city = "";
		/** 工作性质 */
		public String employeeType = "";
		/** 单位名称 */
		public String corpName = "";
		/** 是否有本地房产 */
		public String localHouseFlag = "";
		/** 居住情况 */
		public String livingCondition = "";
		/** 婚姻状况 */
		public String marriage = "";
		/** 教育程度 */
		public String educationLevel = "";
		/** 客户经理编号 */
		public String customerMgr = "";
		/** 0：不需要补件 1：补充征信报告 2：补充征信授权 */
		public String supplementType = "";
		/** 0：不可查看征信 1：可查看有通道征信 2：可查看无通道征信 */
		public String creditType = "";
		/** 1：云测推荐 2：云贷推荐 */
		public String rcmdType = "";
		/** 退回原因（补件时需要） */
		public String reason = "";
		/** 推荐编号 */
		public String rcmdId = "";
		/**  是否能查看历史进度 */
		public boolean historyType;
		/** 是否已试算额度 */
		public String isCalculator="";
		/** 产品类型 */
		public String loantype ="";
		/** 最大额度 */
		public String maxAmount ="";
		/** 额度试算H5 url */
		public String calculatorUrl ="";

		public DetailBean() {
		}

		protected DetailBean(Parcel in) {
			status = in.readString();
			rcmdTime = in.readString();
			statusTip = in.readString();
			name = in.readString();
			certId = in.readString();
			phone = in.readString();
			city = in.readString();
			employeeType = in.readString();
			corpName = in.readString();
			localHouseFlag = in.readString();
			livingCondition = in.readString();
			marriage = in.readString();
			educationLevel = in.readString();
			customerMgr = in.readString();
			supplementType = in.readString();
			creditType = in.readString();
			rcmdType = in.readString();
			reason = in.readString();
			rcmdId = in.readString();
		}

		@Override
		public void writeToParcel(Parcel dest, int flags) {
			dest.writeString(status);
			dest.writeString(rcmdTime);
			dest.writeString(statusTip);
			dest.writeString(name);
			dest.writeString(certId);
			dest.writeString(phone);
			dest.writeString(city);
			dest.writeString(employeeType);
			dest.writeString(corpName);
			dest.writeString(localHouseFlag);
			dest.writeString(livingCondition);
			dest.writeString(marriage);
			dest.writeString(educationLevel);
			dest.writeString(customerMgr);
			dest.writeString(supplementType);
			dest.writeString(creditType);
			dest.writeString(rcmdType);
			dest.writeString(reason);
			dest.writeString(rcmdId);
		}

		@Override
		public int describeContents() {
			return 0;
		}

		public static final Creator<DetailBean> CREATOR = new Creator<DetailBean>() {
			@Override
			public DetailBean createFromParcel(Parcel in) {
				return new DetailBean(in);
			}

			@Override
			public DetailBean[] newArray(int size) {
				return new DetailBean[size];
			}
		};
	}

}
